package com.wuhuahe.learning.lagou.service.user.service.impl;

import com.wuhuahe.learning.lagou.service.user.mapper.UserMapper;
import com.wuhuahe.learning.lagou.service.user.pojo.RegisterInfo;
import com.wuhuahe.learning.lagou.service.user.pojo.User;
import com.wuhuahe.learning.lagou.service.user.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public boolean register(RegisterInfo registerInfo) {
        User user = new User();
        user.setEmail(registerInfo.getEmail());
        user.setPassword(registerInfo.getPassword());
        Timestamp timestamp = new Timestamp(new Date().getTime());
        user.setCreated_date(timestamp);
        user.setUpdated_date(timestamp);
        try {
            int userId = userMapper.insertUser(user);
            return true;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean isRegistered(String email) {

        User user = userMapper.findByEmail(email);
        return user != null;
    }

    @Override
    public User getUserByEmailAndPassword(String email, String password) {
        User user = userMapper.findByEmailAndPassword(email, password);
        return user;
    }
}
